package com.prs.crm.action.crm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.RivalLevel;
import com.prs.crm.service.crm.RivalLevelService;
import com.prs.crm.service.sys.LogService;

public class RivalLevelAction extends BaseAction {
	private RivalLevel rivalLevel;
	private RivalLevelService rivalLevelService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private Collection<TreeNode> nodes;
	private LogService logService;

	@Action("listRivalLevel")
	public String listRivalLevel() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				RivalLevel.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRivalLevelService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editRivalLevel")
	public String editRivalLevel() {
		return SUCCESS;
	}

	@Action(value = "loadRivalLevelTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadRivalLevelTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<RivalLevel> levels = this.getRivalLevelService()
				.getAllRivalLevel();
		for (RivalLevel level : levels) {
			TreeNode node = new TreeNode();
			node.setId(level.getId().toString());
			node.setText(level.getLevel());
			nodes.add(node);
		}
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveRivalLevel", results = {
			@Result(name = "success", type = "redirect", location = "listRivalLevel"),
			@Result(name = "error", type = "chain", location = "editRivalLevel") })
	public String saveRivalLevel() {
		if (rivalLevel.getLevel() == null
				|| "".equals(rivalLevel.getLevel().trim())) {
			this.addActionError("威胁级别不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName1(RivalLevel.class, "level", rivalLevel
				.getLevel())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getRivalLevelService().save(rivalLevel);
		this.setPrompt("提交竞争对手威胁级别成功");
		return SUCCESS;
	}

	@Action(value = "removeRivalLevel", results = {
			@Result(name = "success", type = "redirect", location = "listRivalLevel"),
			@Result(name = "error", type = "chain", location = "listRivalLevel") })
	public String removeRivalLevel() {
		if (ids == null) {
			this.addActionError("请选择要删除的竞争对手威胁级别");
			return ERROR;
		}
		for (Integer id : ids) {
			RivalLevel level = this.getRivalLevelService().get(id);
			if (level.getRivals().size() != 0) {
				this.addActionError("已有竞争对手引用，不能删除");
				return ERROR;
			}
		}
		this.getRivalLevelService().batchRemove(ids);
		return SUCCESS;
	}

	public RivalLevel getRivalLevel() {
		return rivalLevel;
	}

	public void setRivalLevel(RivalLevel rivalLevel) {
		this.rivalLevel = rivalLevel;
	}

	public RivalLevelService getRivalLevelService() {
		return rivalLevelService;
	}

	public void setRivalLevelService(RivalLevelService rivalLevelService) {
		this.rivalLevelService = rivalLevelService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
